package com.jshoperxms.components.aspect;

import com.jshoperxms.controller.api.langdu.vo.YdRecordRdTVo;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.YdEnums;
import com.jshoperxms.entity.YdDailymtaskRdT;
import com.jshoperxms.entity.YdRecordRdT;
import com.jshoperxms.service.YdDailymtaskRdTService;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by sdywcd on 2019/1/8.
 * Des:每日任务的切片处理
 */
@Aspect
@Component
public class DailyTaskAspect {

    @Resource
    private YdDailymtaskRdTService ydDailymtaskRdTService;

    @Pointcut("execution(public * com.jshoperxms.controller.api.langdu.RecordController.saveTeamCustomRecord(*))")
    public void memberSaveTeamCustomRecordRdPointCut(){

    }
    @After(value = "memberSaveTeamCustomRecordRdPointCut()")
    public void doUpdateMemberDailyTaskForTeamCustomYdRecordRd(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        if(args.length>0){
            if(args[0] instanceof YdRecordRdTVo){
                YdRecordRdTVo vo=(YdRecordRdTVo) args[0];
                if(StringUtils.equals(YdEnums.TASKTYPE.VOICETASK.getState(),vo.getTasktype())){
                    //执行每日任务插入动作
                    YdDailymtaskRdT bean=this.ydDailymtaskRdTService.findByPK(YdDailymtaskRdT.class,vo.getTaskid());
                    if(bean!=null){
                        bean.setUpdatetime(DateTools.getSystemTimeLong());
                        bean.setVersiont(bean.getVersiont()+1);
                        bean.setRpactionid(vo.getId());
                        bean.setTaskstatus(YdEnums.TASKSTATUS.FINISH.getState());
                        this.ydDailymtaskRdTService.update(bean);
                    }
                }
            }
        }
    }

    @Pointcut("execution(public * com.jshoperxms.controller.api.langdu.RecordController.saveRecord(*))")
    public void memberSaveYdRecordRdPointCut(){

    }

    /**
     * 发布朗读录音后在每日任务中进行记录
     * @param joinPoint
     */
    @After(value = "memberSaveYdRecordRdPointCut()")
    public void doUpdateMemberDailyTaskForYdRecordRd(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        if(args.length>0){
            if(args[0] instanceof YdRecordRdTVo){
                YdRecordRdTVo vo=(YdRecordRdTVo) args[0];
                if(StringUtils.equals(YdEnums.TASKTYPE.VOICETASK.getState(),vo.getTasktype())){
                    //执行每日任务插入动作
                    YdDailymtaskRdT bean=this.ydDailymtaskRdTService.findByPK(YdDailymtaskRdT.class,vo.getTaskid());
                    if(bean!=null){
                        bean.setUpdatetime(DateTools.getSystemTimeLong());
                        bean.setVersiont(bean.getVersiont()+1);
                        bean.setRpactionid(vo.getId());
                        bean.setTaskstatus(YdEnums.TASKSTATUS.FINISH.getState());
                        this.ydDailymtaskRdTService.update(bean);
                    }
                }
            }
        }
    }


    /**
     * 图文朗读入口切片
     */
    @Pointcut("execution(public * com.jshoperxms.controller.api.langdu.RecordController.saveArtRecord(*))")
    public void memberSaveArtYdRecordRdPointCut(){

    }

    @After(value = "memberSaveArtYdRecordRdPointCut()")
    public void doUpdateMemberDailyTaskForArtYdRecordRd(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        if(args.length>0){
            if(args[0] instanceof YdRecordRdTVo){
                YdRecordRdTVo vo=(YdRecordRdTVo) args[0];
                if(StringUtils.equals(YdEnums.TASKTYPE.PICTURETASK.getState(),vo.getTasktype())){
                    //执行每日任务插入动作
                    YdDailymtaskRdT bean=this.ydDailymtaskRdTService.findByPK(YdDailymtaskRdT.class,vo.getTaskid());
                    if(bean!=null){
                        bean.setUpdatetime(DateTools.getSystemTimeLong());
                        bean.setVersiont(bean.getVersiont()+1);
                        bean.setRpactionid(vo.getId());
                        bean.setTaskstatus(YdEnums.TASKSTATUS.FINISH.getState());
                        this.ydDailymtaskRdTService.update(bean);
                    }
                }
            }
        }
    }
}
